<?php
/**
 * 设置路由 【列表，新增，更新，查询一条，删除，修改状态】，默认没有修改状态
 * @param string $controller
 * @param \Illuminate\Routing\Router $router
 * @param string $name
 * @param array $routes
 */
function setUsualRoutes(string $controller, \Illuminate\Routing\Router $router, string $name, array $routes = BASIC_ROUTES, $extra_routers = [])
{
    $router->group(['prefix' => $name], function (\Illuminate\Routing\Router $router) use ($controller, $name, $routes, $extra_routers) {
        $router->pattern($name, '[0-9]+');
        if (empty($routes) || in_array('list', $routes)) {
            $router->get('', $controller . '@fetchList');
        }
        if (empty($routes) || in_array('create', $routes)) {
            $router->post('', $controller . '@create');
        }
        if (empty($routes) || in_array('update', $routes)) {
            $router->post('{' . $name . '}', $controller . '@update');
        }
        if (empty($routes) || in_array('info', $routes)) {
            $router->get('{' . $name . '}', $controller . '@fetchInfo');
        }
        if (empty($routes) || in_array('delete', $routes)) {
            $router->delete('{' . $name . '}', $controller . '@delete');
        }
        if (in_array('status', $routes)) {
            $router->patch('{' . $name . '}/set-status', $controller . '@setStatus');
        }
        if ($extra_routers) {
            foreach ($extra_routers as $v) {
                $uri = upToLower($v['uri']);
                $router->addRoute(strtoupper($v['method']), $uri, $controller . '@' . $v['action']);
            }
        }
    });
}

Route::group(['middleware' => 'auth:admin'], function (\Illuminate\Routing\Router $router) {
    $router->get('refresh', function (\Illuminate\Routing\Router $router) {
        $nav = config('admin.nav');
        return success(['nav' => $nav], '刷新成功');
    });
    //账号相关
    $router->group(['prefix' => 'auth'], function (\Illuminate\Routing\Router $router) {
        $router->get('info', 'AuthController@info');
        $router->patch('change-password', 'AuthController@changePassword');
        $router->post('logout', 'AuthController@logout');
    });
    $routes = \Illuminate\Support\Facades\Config::get('admin.routes');
    foreach ($routes as $route){
        $router->group([
            'prefix'=>$route['prefix'],
            'namespace'=>$route['namespace']
        ],function(\Illuminate\Routing\Router $router) use($route) {
            foreach ($route['modules'] as $module){
                setUsualRoutes($module['controller'],$router,$module['name'],$module['route_type'],$module['extra']);
            }
        });
    }

});
Route::group([], function (\Illuminate\Routing\Router $router) {
    $router->group(['prefix' => 'auth'], function (\Illuminate\Routing\Router $router) {
        $router->post('login', 'AuthController@login');
    });
    $router->get('csrf', function () {
        return success();
    });
    $router->post('/upload', 'UploadController@index');
});
